package top.laoshuzi.cloudplatform.attitudeseneor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import java.io.IOException;

import top.laoshuzi.cloudplatform.service.Mpu6050;
import top.laoshuzi.cloudplatform.service.Mpu6050Driver;
import top.laoshuzi.cloudplatform.tools.L;

/**
 * Created by mouse on 2017/12/22.
 */

public class AttitudeHanlder implements AutoCloseable, SensorEventListener {

    private static final String I2CBUS_NAME = "I2C1";

    private Mpu6050 mpu6050;
    private Mpu6050Driver mpu6050Driver;
    private SensorManager mSensorManager;

    private SensorManager.DynamicSensorCallback dynamicSensorCallback = new SensorManager.DynamicSensorCallback() {
        @Override
        public void onDynamicSensorConnected(Sensor sensor) {
            mSensorManager.registerListener(
                    AttitudeHanlder.this,
                    sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }

        @Override
        public void onDynamicSensorDisconnected(Sensor sensor) {
            mSensorManager.unregisterListener(AttitudeHanlder.this);
        }
    };

    private AttitudeHanlder() {
    }

    public static AttitudeHanlder getInstance() {
        return InstanceHodler.INSTANCE;
    }

    private static class InstanceHodler {
        private static final AttitudeHanlder INSTANCE = new AttitudeHanlder();
    }

    public void initialize(Context context) throws IOException {
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerDynamicSensorCallback(dynamicSensorCallback);

        mpu6050 = new Mpu6050(I2CBUS_NAME);
        mpu6050Driver = new Mpu6050Driver(mpu6050);
        mpu6050Driver.register();
    }


    @Override
    public void close() throws Exception {
        mSensorManager.unregisterDynamicSensorCallback(dynamicSensorCallback);
        try {
            mpu6050Driver.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getName().equals(Mpu6050Driver.DRIVER_NAME)) {
            L.w("Acc:\t\t"
                    + String.format("%.2f", event.values[0])
                    + "\t\t"
                    + String.format("%.2f", event.values[1])
                    + "\t\t"
                    + String.format("%.2f", event.values[2])
            );
//            L.w("Gyro:\t\t"
//                    + String.format("%.2f", event.values[3])
//                    + "\t\t"
//                    + String.format("%.2f", event.values[4])
//                    + "\t\t"
//                    + String.format("%.2f", event.values[5])
//            );
//            L.w("Temp:\t\t"
//                    + String.format("%.2f", event.values[6])
//            );
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
